#include <myhead.h>
#include "myprotocol.h"
#include "cgic.h"
#define MYPATH "/home/linux"
// char ID[20] = {0};
// char PS[20] = {0};
// char id[20] = "admin";
// char ps[20] = "888888";

int key;
int msgid;
myprotocol_t buf;

// int semid;

// char tem[32];
// char hum[32];
// char light[32];

long myuid;

char clean_buf[1024] = {0};
void alarm_handler(int arg)
{
    // 非阻塞的方式读走消息队列中的消息
    msgrcv(msgid, clean_buf, sizeof(clean_buf), buf.msgtype, IPC_NOWAIT);

    printf("Content-type: text/html;charset=\"UTF-8\"\n\n");
    printf("<!DOCTYPE html>");
    printf("<html>");
    printf("<body>");
    printf("<center>");
    printf("<h1>网络似乎不太好</h1>"); // 输出错误信息( 大标题 )
    printf("<br>");                    // 换行
    // printf("<a href=\"/index.html\">返回重新输入</a>");
    printf("</center>");
    printf("</body>");
    printf("</html>");
    exit(-1);
}
int cgiMain(int argc, const char *argv[])
{
    signal(SIGALRM, alarm_handler);
    myuid = 0;

    key = ftok(MYPATH, 'A');
    if (-1 == key)
    {
        perror("ftok error");
        return -1;
    }
    msgid = msgget(key, IPC_CREAT | 0666);
    if (-1 == msgid)
    {
        perror("msgget error");
        return -1;
    }

    memset(&buf, 0, sizeof(buf));

    char cp_res[32] = {0};
    char cookiebuf[20] = {0};
    memcpy(cookiebuf, cgiCookie, 20);

    char *temp = cookiebuf;
    int i = 0;
    while (*temp)
    {
        if (*temp == '=')
        {
            temp++;
            while (*temp)
            {
                cp_res[i] = *temp;
                i++;
                temp++;
            }
            break;
        }
        temp++;
    }
    cp_res[i] = '\0';

    temp = cp_res;
    while (*temp)
    {
        myuid += *temp;
        temp++;
    }
    buf.uid = myuid;
    buf.msgtype = myuid;
    buf.cmd = GET_ENVVAL_REQ;

    if (-1 == msgsnd(msgid, &buf, sizeof(buf) - sizeof(buf.msgtype), 0))
    {
        perror("msgsnd error");
        return -1;
    }
    alarm(4);
    if (-1 == msgrcv(msgid, &buf, sizeof(buf) - sizeof(buf.msgtype), myuid * 2, 0))
    {
        perror("msgrcv error");
        return -1;
    }
    
    printf("Content-type: text/html;charset=\"UTF-8\"\n\n"); // 固定格式 必须要加
    printf("<!DOCTYPE html>\n");
    printf("<html>\n");
    printf("<body>\n");
    printf("<center>\n");

    printf("<table border='4'>\n");
    printf("<th width='80' height='20'>用户ID</th>\n");
    printf("<th width='60' height='20'>%s</th>\n",cp_res);
    printf("</table>\n");

    printf("<h3>环境数据</h3>\n");
    printf("<table bgcolor='Azure' border='4'>\n");
    printf("<th width='50' height='20'></th>\n");
    printf("<th width='50' height='20'>温度</th>\n");
    printf("<th width='50' height='20'>湿度</th>\n");
    printf("<th width='50' height='20'>光强</th>\n");
    printf("<tr height='20'>\n");
    printf("<td><center>上限</center></td>\n");
    printf("<td><center>%.2f°C</center></td>\n", buf.threshold_info.tem_max);
    printf("<td><center>%.2f%%</center></td>\n", buf.threshold_info.hum_max);
    printf("<td><center>%.2flx</center></td>\n", buf.threshold_info.lx_max);
    printf("</tr>\n");
    printf("<tr height='20'>\n");
    printf("<td><center>当前</center></td>\n");
    printf("<td><center>%.2f°C</center></td>\n", buf.env_info.tem);
    printf("<td><center>%.2f%%</center></td>\n", buf.env_info.hum);
    printf("<td><center>%.2flx</center></td>\n", buf.env_info.lx);
    printf("</tr>\n");
    printf("<tr height='20'>\n");
    printf("<td><center>下限</center></td>\n");
    printf("<td><center>%.2f°C</center></td>\n", buf.threshold_info.tem_min);
    printf("<td><center>%.2f%%</center></td>\n", buf.threshold_info.hum_min);
    printf("<td><center>%.2flx</center></td>\n", buf.threshold_info.lx_min);
    printf("</tr>\n");
    printf("</table>\n");

    printf("<h3>设备状态</h3>\n");
    printf("<table bgcolor='Azure' border='4'>\n");
    printf("<th width='50' height='20'></th>\n");
    printf("<th width='50' height='20'>照明</th>\n");
    printf("<th width='50' height='20'>温度</th>\n");
    printf("<th width='50' height='20'>湿度</th>\n");
    printf("<tr height='20'>\n");
    printf("<td><center>状态</center></td>\n");

    if(buf.env_info.dev_state & 0x01)
    {
        printf("<td><center>开</center></td>\n");
    }
    else
    {
        printf("<td><center>关</center></td>\n");
    }
    if(((buf.env_info.dev_state >> 1) & 0x03) == 0x01) // 不加括号会出错 因为关系运算符优先级高于位运算符
    {
        printf("<td><center>1档</center></td>\n");
    }
    else if(((buf.env_info.dev_state >> 1) & 0x03) == 0x02)
    {
        printf("<td><center>2档</center></td>\n");
    }
    else if(((buf.env_info.dev_state >> 1) & 0x03) == 0x03)
    {
        printf("<td><center>3档</center></td>\n");
    }
    else
    {
        printf("<td><center>关</center></td>\n");
    }
    if((buf.env_info.dev_state >> 3) & 0x01)
    {
        printf("<td><center>开</center></td>\n");
    }
    else
    {
        printf("<td><center>关</center></td>\n");
    }
    
    printf("</tr>\n");
    printf("</table>\n");

    printf("<br>\n");
    printf("<form action='/cgi-bin/living_view.cgi' method='get'>\n");
    printf("<input type='submit' name='flash_button' value='刷新数据' />\n");
    printf("</form>\n");

    printf("</center>\n");
    printf("</body>\n");
    printf("</html>\n");

    return 0;
}
